package com.wanghengzhi.kata;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wanghengzhi
 * @since 2021/11/16 21:07
 */
public class Anagrams {

    public Set<String> generate(String word) {
        if (word.length() == 1) {
            return Collections.singleton(word);
        }
        Set<String> result = new HashSet<>();
        for (int i = 0; i < word.length(); i++) {
            Set<String> left = generate(removeIndexChar(word, i));
            for (String leftStr : left) {
                result.add(word.charAt(i) + leftStr);
            }
        }
        return result;
    }

    private String removeIndexChar(String word, int charAt) {
        return new StringBuilder(word).deleteCharAt(charAt).toString();
    }

}
